<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="85px">
      <el-form-item label="公司名称" prop="companynameId">
        <el-input
          v-model="queryParams.companynameId"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="compamyDept">
        <el-input
          v-model="queryParams.compamyDept"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="部门长姓名" prop="name">-->
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入部门长姓名"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门长电话" prop="phone">-->
<!--        <el-input-->
<!--          v-model="queryParams.phone"-->
<!--          placeholder="请输入部门长电话"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门长证件号码" prop="idnumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.idnumber"-->
<!--          placeholder="请输入部门长证件号码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="进场日期" prop="starttime">-->
<!--        <el-date-picker clearable size="small" style="width: 200px"-->
<!--          v-model="queryParams.starttime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择进场日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="班组长住址" prop="address">-->
<!--        <el-input-->
<!--          v-model="queryParams.address"-->
<!--          placeholder="请输入班组长住址"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="请求序列编码" prop="requestcode">-->
<!--        <el-input-->
<!--          v-model="queryParams.requestcode"-->
<!--          placeholder="请输入请求序列编码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="班组编号" prop="teamsysno">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamsysno"-->
<!--          placeholder="请输入班组编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="大屏是否显示" prop="isDisplayLed">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDisplayLed"-->
<!--          placeholder="请输入大屏是否显示"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="显示排序" prop="sortNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.sortNum"-->
<!--          placeholder="请输入显示排序"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="crtime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.crtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lgdz:companydept:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lgdz:companydept:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lgdz:companydept:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lgdz:companydept:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="companydeptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />-->
      <el-table-column label="公司名称" align="center" prop="companynameId" />
      <el-table-column label="部门名称" align="center" prop="compamyDept" />
<!--      <el-table-column label="部门长姓名" align="center" prop="name" />-->
<!--      <el-table-column label="部门长电话" align="center" prop="phone" />-->
<!--      <el-table-column label="部门长证件号码" align="center" prop="idnumber" />-->
<!--      <el-table-column label="进场日期" align="center" prop="starttime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.starttime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="班组长住址" align="center" prop="address" />-->
<!--      <el-table-column label="请求序列编码" align="center" prop="requestcode" />-->
<!--      <el-table-column label="班组编号" align="center" prop="teamsysno" />-->
<!--      <el-table-column label="大屏是否显示" align="center" prop="isDisplayLed" :formatter="isDisplayLedFormat" />-->
      <el-table-column label="显示排序" align="center" prop="sortNum" />
      <el-table-column label="创建时间" align="center" prop="crtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="uptime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uptime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lgdz:companydept:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lgdz:companydept:remove']"
          >删除</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="getComip"-->
<!--          >测试</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="公司名称" prop="companynameId">
<!--          <el-input v-model="form.companynameId" placeholder="请输入公司名称" />-->
          <el-select placeholder="请选择公司名称" v-model="form.companynameId" style="width: 100%">
            <el-option :key="dist.id" :label="dist.companyName" :value="dist.id" v-for="dist in yarg"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="compamyDept">
          <el-input v-model="form.compamyDept" placeholder="请输入部门名称" />
        </el-form-item>
<!--        <el-form-item label="部门长姓名" prop="name">-->
<!--          <el-input v-model="form.name" placeholder="请输入部门长姓名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="部门长电话" prop="phone">-->
<!--          <el-input v-model="form.phone" placeholder="请输入部门长电话" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="部门长证件号码" prop="idnumber">-->
<!--          <el-input v-model="form.idnumber" placeholder="请输入部门长证件号码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="进场日期" prop="starttime">-->
<!--          <el-date-picker clearable size="small" style="width: 200px"-->
<!--            v-model="form.starttime"-->
<!--            type="date"-->
<!--                          :style="{width: '100%'}"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择进场日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="班组长住址" prop="address">-->
<!--          <el-input v-model="form.address" placeholder="请输入班组长住址" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="请求序列编码" prop="requestcode">-->
<!--          <el-input v-model="form.requestcode" placeholder="请输入请求序列编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="班组编号" prop="teamsysno">-->
<!--          <el-input v-model="form.teamsysno" placeholder="请输入班组编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="大屏是否显示" prop="isDisplayLed">-->
<!--          <el-input v-model="form.isDisplayLed" placeholder="请输入大屏是否显示" />-->
<!--        </el-form-item>-->
        <el-form-item label="显示排序" prop="sortNum">
<!--          <el-input v-model="form.sortNum" placeholder="请输入显示排序" :min="0"/>-->
          <el-input-number v-model="form.sortNum" controls-position="right" :min="0" />
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="crtime">-->
<!--          <el-date-picker clearable size="small" style="width: 200px"-->
<!--            v-model="form.crtime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择创建时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="修改时间" prop="uptime">-->
<!--          <el-date-picker clearable size="small" style="width: 200px"-->
<!--            v-model="form.uptime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择修改时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompanydept, getCompanydept, delCompanydept, addCompanydept, updateCompanydept, exportCompanydept } from "@/api/lgdz/companydept";
import { listCompanyname } from "@/api/lgdz/companyname";
export default {
  name: "Companydept",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      yarg:[],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 公司部门表格数据
      companydeptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 大屏是否显示字典
      isDisplayLedOptions: [],
      // 显示排序字典
      sortNumOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companynameId: undefined,
        compamyDept: undefined,
        name: undefined,
        phone: undefined,
        idnumber: undefined,
        starttime: undefined,
        address: undefined,
        requestcode: undefined,
        teamsysno: undefined,
        isDisplayLed: undefined,
        sortNum: undefined,
        crtime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        compamyDept: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getListname()

    this.getDicts("companyDept_Is_Display_Led").then(response => {
      this.isDisplayLedOptions = response.data;
    });
    this.getDicts("companyDept_Sort_Num").then(response => {
      this.sortNumOptions = response.data;
    });
  },
  methods: {
    // 根据id查公司
    getComip(){
      const api = "http://192.168.10.175:8088/device/queryCompanyName";
      this.$axios({
        url: api,
        method: "get",
        params:{
          companyId:this.companydeptList.companynameId
        }
        //用于表示用户代理是否在跨域请求的情况下从其他域中发送cookies
        //  withCredentials:true,//默认为false
      }).then((res) => {
        console.log(res);
      })
    },
    /** 查询公司名称列表 */
    getListname() {
      listCompanyname(this.queryParams).then(response => {
        // console.log(response)
        this.yarg = response.rows;
      });
    },
    /** 查询公司部门列表 */
    getList() {
      this.loading = true;
      listCompanydept(this.queryParams).then(response => {
        // console.log(response)
        this.companydeptList = response.rows;
        this.companydeptList.forEach((item)=>{
            const api = "http://192.168.10.175:8088/device/queryCompanyName";
            this.$axios({
              url: api,
              method: "get",
              params:{
                companyId:item.companynameId
              }
              //用于表示用户代理是否在跨域请求的情况下从其他域中发送cookies
              //  withCredentials:true,//默认为false
            }).then((res) => {
              // console.log(res.data);
             item.companynameId=res.data
            })

        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 大屏是否显示字典翻译
    isDisplayLedFormat(row, column) {
      return this.selectDictLabel(this.isDisplayLedOptions, row.isDisplayLed);
    },
    // 显示排序字典翻译
    sortNumFormat(row, column) {
      return this.selectDictLabel(this.sortNumOptions, row.sortNum);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companynameId: undefined,
        compamyDept: undefined,
        name: undefined,
        phone: undefined,
        idnumber: undefined,
        starttime: undefined,
        address: undefined,
        requestcode: undefined,
        teamsysno: undefined,
        isDisplayLed: undefined,
        sortNum: undefined,
        crtime: undefined,
        uptime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司部门";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // console.log(row)
      this.reset();
      const id = row.id || this.ids
      getCompanydept(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司部门";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCompanydept(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCompanydept(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公司部门编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompanydept(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有公司部门数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompanydept(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
